package com.jeff.tianti.controller;

import com.jeff.tianti.cms.entity.GateUser;
import com.jeff.tianti.cms.service.GateUserService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.utils.redis.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@CrossOrigin(maxAge = 3600)
public class GateUserController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    GateUserService gateUserService;

    @RequestMapping(value = "/reg", method = RequestMethod.POST)
    public AjaxResult reg(@RequestBody GateUser gateUser) throws RuntimeException {
        GateUser user = gateUserService.getByMobile(gateUser.getMobile());
        if (user != null) {
            throw new RuntimeException("该用户已存在");
        }
        String code = gateUser.getCode();
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.setRedisTemplate(redisTemplate);
        String oleCode = (String) redisUtil.get(gateUser.getMobile());
        if (StringUtils.isBlank(oleCode)) {
            throw new RuntimeException("验证码已过期");
        }
        if (!oleCode.equals(code)) {
            throw new RuntimeException("验证码输入有误");
        }
        gateUserService.save(gateUser);
        return AjaxResult.setSuccessData(gateUser);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public AjaxResult login(@RequestBody GateUser gateUser) throws RuntimeException {
        GateUser user = gateUserService.getByMobile(gateUser.getMobile());
        if (user == null) {
            throw new RuntimeException("该用户不存在");
        }
        String pwd = gateUser.getPassword();
        if (!pwd.equals(user.getPassword())) {
            throw new RuntimeException("密码不正确");
        }
        return AjaxResult.setSuccessData(user);
    }

    @RequestMapping(value = "/forget/pwd", method = RequestMethod.POST)
    public AjaxResult forget(@RequestBody GateUser gateUser) throws RuntimeException {

        String mobile = gateUser.getMobile();
        String password = gateUser.getPassword();
        if (StringUtils.isBlank(mobile)) {
            throw new RuntimeException("请输入手机号");
        }
        if (StringUtils.isBlank(password)) {
            throw new RuntimeException("请输入密码");
        }
        GateUser user = gateUserService.getByMobile(mobile);
        if (user == null) {
            throw new RuntimeException("该用户不存在");
        }
        String code = gateUser.getCode();
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.setRedisTemplate(redisTemplate);
        String oleCode = (String) redisUtil.get(mobile);
        if (StringUtils.isBlank(oleCode)) {
            throw new RuntimeException("验证码已过期");
        }
        if (!code.equals(oleCode)) {
            throw new RuntimeException("验证码输入有误");
        }
        user.setPassword(password);
        gateUserService.update(user);
        return AjaxResult.setSuccessData(user);
    }
}
